package com.ravi.agent;

import com.ravi.common.constant.CheckExceptionEnum;
import com.ravi.common.exception.CheckException;
import com.ravi.mapper.PatientMapper;
import com.ravi.mapper.RoomTypeMapper;
import com.ravi.model.entity.Patient;
import com.ravi.queue.StringQueueUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PhysicalAlgorithm {

	@Autowired
	private RoomTypeMapper roomTypeMapper;

	@Autowired
	private StringQueueUtil stringQueueUtil;

	@Autowired
	private PatientMapper patientMapper;

	public String manage(String checkupCode) {
		// 如果还未完成空腹项目
		Patient patient = patientMapper.selectOneByCheckCode(checkupCode);
		if (ObjectUtils.isEmpty(patient)) {
			throw new CheckException(CheckExceptionEnum.PATIENT_NOT_FOUND);
		}
		Long chouxie = stringQueueUtil.getQueueLength("CHOUXIE");
		Long caichao = Long.parseLong("0");
		if (patient.getSex().equals("女")) {
			caichao = stringQueueUtil.getQueueLength("CAICHAO1");
		}else {
			caichao = stringQueueUtil.getQueueLength("CAICHAO2");
		}

		Boolean flag = chouxie > caichao *3;
		if (flag) {
			switch (patient.getSex()) {
				case "男":
					stringQueueUtil.addPatient2Queue("CAICHAO2", checkupCode);
					return "CAICHAO2";
				case "女":
					stringQueueUtil.addPatient2Queue("CAICHAO1", checkupCode);
					return "CAICHAO1";
			}
		}else {
			stringQueueUtil.addPatient2Queue("CHOUXIE", checkupCode);
			return "CHOUXIE";
		}
		return null;
	}
}
